package csse3004.GD.PM;

/**
 * @author David Bullock S4206222
 */
import csse3004.GD.DA.PollMasterDA;
import org.springframework.validation.Validator;
import org.springframework.validation.Errors;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class MessageValidator implements Validator {

    protected final Log logger = LogFactory.getLog(getClass());
    private PollMasterService service = new PollMasterDA();

    public boolean supports(Class clazz) {
        return Message.class.equals(clazz);
    }

    public void validate(Object obj, Errors errors) {

        Message message = (Message) obj;
        String uid = message.getUid();
        String txt = message.getMessage();
        
        if(txt.isEmpty()){
            errors.rejectValue("txtErr", "error.not-specified", null, "Please fill in the title then re-submit!");
        }
        try {
            Integer.parseInt(uid);
        } catch (NumberFormatException E) {
            errors.rejectValue("uidErr", "error.not-specified", null, "Must be integer");
        }

    }
}
